package com.day4.system.controller;

import com.day4.system.domain.Teacher;
import com.day4.system.service.TeacherService;

import java.util.Scanner;

/**
 * @author 小坡
 * @date 2022/8/25  18:14
 **/
public class TeacherController {

    private Scanner scanner = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();

    public void start() {

        stopteacher:
        while (true) {
            System.out.println("--------欢迎来到〈老师>管理系统--------");
            System.out.println("请输入您的选择:1.添加老师⒉.删除老师3.修改老师﹑4.查看老师―5.退出");
            String chioce = scanner.next();
            switch (chioce) {
                case "1":
                    System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
                    System.out.println("删除老师");
                    delTeacher();
                    break;
                case "3":
                    System.out.println("修改老师");
                    updateTeacher();
                    break;
                case "4":
                    System.out.println("查看老师");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("退出");
                    break stopteacher;
                default:
                    System.out.println("输入格式有误，请重新输入");
                    break;
            }
        }
    }

    public void updateTeacher() {
      /*  String id;
        while (true) {
            //        1、键盘接受要删除的老师的id
            System.out.println("请输入要修改的老师的id");
            id = scanner.next();
            boolean exists = teacherService.isExists(id);
            if (!exists) {
                System.out.println("id不存在，请重新输入");
            } else {
                break;
            }
        }*/
        String id=inputTeacher();
/*//        键盘录入新的老师信息
        System.out.println("请输入老师的姓名");
        String name = scanner.next();
        System.out.println("请输入老师的年龄：");
        String age = scanner.next();
        System.out.println("请输入老师的生日：");
        String birthday = scanner.next();
//        封装为新的老师对象
        Teacher newTeacher = new Teacher();
        newTeacher.setId(id);
        newTeacher.setName(name);
        newTeacher.setAge(age);
        newTeacher.setBirthday(birthday);*/
        Teacher newTeacher=inputTeacherInfo(id);
//        调用业务员的修改发方法
        teacherService.updateTeacher(id,newTeacher);
//        提示
        System.out.println("修改成功");
    }

    public void delTeacher() {
       /* String id;
        while (true) {
            //        1、键盘接受要删除的老师的id
            System.out.println("请输入要删除的老师的id");
            id = scanner.next();
            boolean exists = teacherService.isExists(id);
            if (!exists) {
                System.out.println("id不存在，请重新输入");
            } else {
                break;
            }
        }*/
        String id=inputTeacher();
//        2、调用业务员中的删除方法   根据id  删除老师
        teacherService.delTeacher(id);
//        3、删除成功的提示
        System.out.println("删除成功");
    }

    //查看老师
    public void findAllTeacher() {
//        1、从业务员中获取一个老师对象数组
        Teacher[] teachers = teacherService.findAllTeacher();
//        2、判断数组中是否又元素
        if (teachers == null) {
            System.out.println("查无信息，添加后重试");
            return;
        }
//        遍历数组，取出元素，并打印在控制台
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null) {
                System.out.println(teacher.getId() + "\t" + teacher.getName() + "\t" + teacher.getAge() + "\t\t" + teacher.getBirthday());
            }
        }
    }

    public void addTeacher() {
        String id;
//不确定输入的id是否存在     如果存在需要进行重新输入的操作
        while (true) {
            //     1、   接受一个不存在的老师id
            System.out.println("请输入老师的id：");
            id = scanner.next();
//         2、   判断id是否存在   这是一个业务  需要业务员
            boolean exists = teacherService.isExists(id);
            if (exists) {
                System.out.println("id已经被占用，请重新输入");
            } else {
//                不存在   进行录入
                break;
            }
        }
/*//        3、接受老师的其他信息
        System.out.println("请输入老师的姓名：");
        String name = scanner.next();
        System.out.println("请输入老师的年龄：");
        String age = scanner.next();
        System.out.println("请输入老师的生日：");
        String birthday = scanner.next();
//        4、封装为老师对象
        Teacher teacher = new Teacher();
        teacher.setId(id);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);*/
        Teacher teacher=inputTeacherInfo(id);
//        5、将封装好的老师对象，传递给TeacherService继续完成添加操作
        boolean result = teacherService.addTeacher(teacher);
        if (result) {
            System.out.println("添加成功");
//            System.out.println("姓名为：=" + name);
        } else {
            System.out.println("添加失败");
        }

    }
//    录入老师的id
    public String  inputTeacher(){
        String id;
        while (true) {
            //        1、键盘接受要删除的老师的id
            System.out.println("请输入要删除的老师的id");
            id = scanner.next();
            boolean exists = teacherService.isExists(id);
            if (!exists) {
                System.out.println("id不存在，请重新输入");
            } else {
                break;
            }
        }
        return id;
    }
//    录入老师信息，封装为老师类
    public Teacher inputTeacherInfo(String id){
//        3、接受老师的其他信息
        System.out.println("请输入老师的姓名：");
        String name = scanner.next();
        System.out.println("请输入老师的年龄：");
        String age = scanner.next();
        System.out.println("请输入老师的生日：");
        String birthday = scanner.next();
//        4、封装为老师对象
        Teacher teacher = new Teacher();
        teacher.setId(id);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);
        return teacher;
    }

}
